# package
require("readr")
require("dplyr")
require("tidyr")
require("ggplot2")
theme_set(theme_minimal(base_size = 12, base_family = 'Calibri'))


# load
UTAS2020 <- read_csv("http://www.masaki.j.u-tokyo.ac.jp/utas/2020UTASV20210319.csv", 
                     locale = locale(encoding = "CP932"))


# explanatory variable
## term based arrival date
UTAS2020$Term <- as.character(UTAS2020$Term)
table(UTAS2020$Term, useNA = "always")


# outcome variables
## 1. Japan should increase its defensive power.
UTAS2020$Preference1 <- UTAS2020$Q32_1
UTAS2020$Preference1[UTAS2020$Q32_1 == 99] <- NA
table(UTAS2020$Preference1, useNA = "always")

## 2. If it is expected that Japan will be attacked by another country, Japan should not hesitate to take a preemptive strike.
UTAS2020$Preference2 <- UTAS2020$Q32_2
UTAS2020$Preference2[UTAS2020$Q32_2 == 99] <- NA
table(UTAS2020$Preference2, useNA = "always")

## 3. For North Korea, it is better to apply pressure than to have bilateral dialogue.
UTAS2020$Preference3 <- UTAS2020$Q32_3
UTAS2020$Preference3[UTAS2020$Q32_3 == 99] <- NA
table(UTAS2020$Preference3, useNA = "always")

## 4. Even if services such as social welfare worsen, small government that does not cost money is better.
UTAS2020$Preference4 <- UTAS2020$Q32_4
UTAS2020$Preference4[UTAS2020$Q32_4 == 99] <- NA
table(UTAS2020$Preference4, useNA = "always")
prop.table(table(UTAS2020$Preference4[UTAS2020$Term == "1" & !is.na(UTAS2020$Preference4)], useNA = "always"))

## 5. Rural employment should be boosted by public works.
UTAS2020$Preference5 <- UTAS2020$Q32_5
UTAS2020$Preference5[UTAS2020$Q32_5 == 99] <- NA
table(UTAS2020$Preference5, useNA = "always")

## 6. Instead of focusing on fiscal austerity, Japan needs to boost its economy through increased spending.
UTAS2020$Preference6 <- UTAS2020$Q32_6
UTAS2020$Preference6[UTAS2020$Q32_6 == 99] <- NA
table(UTAS2020$Preference6, useNA = "always")

## 7. It is necessary that consumption tax go over 10%.
UTAS2020$Preference7 <- UTAS2020$Q32_7
UTAS2020$Preference7[UTAS2020$Q32_7 == 99] <- NA
table(UTAS2020$Preference7, useNA = "always")

## 8. To keep order, it is unavoidable to limit personal or privacy rights.
UTAS2020$Preference8 <- UTAS2020$Q32_8
UTAS2020$Preference8[UTAS2020$Q32_8 == 99] <- NA
table(UTAS2020$Preference8, useNA = "always")

## 9.	Same-sex marriage should be recognized.
UTAS2020$Preference9 <- UTAS2020$Q32_9
UTAS2020$Preference9[UTAS2020$Q32_9 == 99] <- NA
table(UTAS2020$Preference9, useNA = "always")

## 10. A selective dual-surname system should be recognized.
UTAS2020$Preference10 <- UTAS2020$Q32_10
UTAS2020$Preference10[UTAS2020$Q32_10 == 99] <- NA
table(UTAS2020$Preference10, useNA = "always")


# control variables
## female
UTAS2020$Q37[UTAS2020$Q37 == 99] <- NA
UTAS2020$Female <- UTAS2020$Q37 - 1
table(UTAS2020$Female, useNA = "always")

## age
UTAS2020$Q38[UTAS2020$Q38 == 99] <- NA
UTAS2020$Age <- UTAS2020$Q38
table(UTAS2020$Age, useNA = "always")

## education
UTAS2020$Q41[UTAS2020$Q41 == 7] <- NA
UTAS2020$Education <- UTAS2020$Q41
table(UTAS2020$Education, useNA = "always")

## self employed
UTAS2020$SelfEmployed <- ifelse(UTAS2020$Q39_1 == 3, 1, 0)
UTAS2020$SelfEmployed[UTAS2020$Q39_1 == 99] <- NA
table(UTAS2020$SelfEmployed, useNA = "always")

## unemployed
UTAS2020$Unemployed <- ifelse(UTAS2020$Q39_1 == 6, 1, 0)
UTAS2020$Unemployed[UTAS2020$Q39_1 == 99] <- NA
table(UTAS2020$Unemployed, useNA = "always")

## standard of living
UTAS2020$Q44[UTAS2020$Q44 == 99] <- NA
UTAS2020$Living <- UTAS2020$Q44
table(UTAS2020$Living, useNA = "always")

## partisanship
UTAS2020$Partisanship <- NA
UTAS2020$Partisanship[UTAS2020$Q34 == 1] <- "LDP"
UTAS2020$Partisanship[UTAS2020$Q34 == 2] <- "CDP"
UTAS2020$Partisanship[UTAS2020$Q34 == 3] <- "NDP"
UTAS2020$Partisanship[UTAS2020$Q34 == 4] <- "Komei"
UTAS2020$Partisanship[UTAS2020$Q34 == 5] <- "JCP"
UTAS2020$Partisanship[UTAS2020$Q34 == 6] <- "JIP"
UTAS2020$Partisanship[UTAS2020$Q34 == 7] <- "SDP"
UTAS2020$Partisanship[UTAS2020$Q34 == 9] <- "NHK"
UTAS2020$Partisanship[UTAS2020$Q34 == 10] <- "Reiwa"
UTAS2020$Partisanship[UTAS2020$Q34 == 11] <- "Others"
UTAS2020$Partisanship[UTAS2020$Q34 == 12] <- "Independent"
UTAS2020$Partisanship <- factor(UTAS2020$Partisanship, 
                                levels = c("LDP", "CDP", "NDP", "Komei", "JCP", "JIP", 
                                           "SDP", "NHK", "Reiwa", "Others", "Independent"))
table(UTAS2020$Partisanship, useNA = "always")

## residential area
UTAS2020$PR_Area <- NA
UTAS2020$PR_Area[UTAS2020$PR == 1] <- "Hokkaido"
UTAS2020$PR_Area[UTAS2020$PR == 2] <- "Tohoku"
UTAS2020$PR_Area[UTAS2020$PR == 3] <- "NorthKanto"
UTAS2020$PR_Area[UTAS2020$PR == 4] <- "SouthKanto"
UTAS2020$PR_Area[UTAS2020$PR == 5] <- "Tokyo"
UTAS2020$PR_Area[UTAS2020$PR == 6] <- "Hokuriku"
UTAS2020$PR_Area[UTAS2020$PR == 7] <- "Tokai"
UTAS2020$PR_Area[UTAS2020$PR == 8] <- "Kinki"
UTAS2020$PR_Area[UTAS2020$PR == 9] <- "Chugoku"
UTAS2020$PR_Area[UTAS2020$PR == 10] <- "Shikoku"
UTAS2020$PR_Area[UTAS2020$PR == 11] <- "Kyushu"
UTAS2020$PR_Area <- factor(UTAS2020$PR_Area, 
                           levels = c("Hokkaido", "Tohoku", "NorthKanto", "SouthKanto", 
                                      "Tokyo", "Hokuriku", "Tokai", "Kinki", "Chugoku", 
                                      "Shikoku", "Kyushu"))
table(UTAS2020$PR_Area, useNA = "always")


# regression: term
## Increasing defensive power
res2_1 <- lm(Preference1 ~ Term + Female + Age + Education + 
               SelfEmployed + Unemployed + Living + Partisanship + PR_Area, 
             data = UTAS2020)
summary(res2_1)

## Pre-emptive defense
res2_2 <- lm(Preference2 ~ Term + Female + Age + Education + 
               SelfEmployed + Unemployed + Living + Partisanship + PR_Area, 
             data = UTAS2020)
summary(res2_2)

## Pressue on North Korea
res2_3 <- lm(Preference3 ~ Term + Female + Age + Education + 
               SelfEmployed + Unemployed + Living + Partisanship + PR_Area, 
             data = UTAS2020)
summary(res2_3)

## Small government
res2_4 <- lm(Preference4 ~ Term + Female + Age + Education + 
               SelfEmployed + Unemployed + Living + Partisanship + PR_Area, 
             data = UTAS2020)
summary(res2_4)

## Public works
res2_5 <- lm(Preference5 ~ Term + Female + Age + Education + 
               SelfEmployed + Unemployed + Living + Partisanship + PR_Area, 
             data = UTAS2020)
summary(res2_5)

## Public spending for economic measures
res2_6 <- lm(Preference6 ~ Term + Female + Age + Education + 
               SelfEmployed + Unemployed + Living + Partisanship + PR_Area, 
             data = UTAS2020)
summary(res2_6)

## Consumption tax over 10%
res2_7 <- lm(Preference7 ~ Term + Female + Age + Education + 
               SelfEmployed + Unemployed + Living + Partisanship + PR_Area, 
             data = UTAS2020)
summary(res2_7)

## Security before privacy
res2_8 <- lm(Preference8 ~ Term + Female + Age + Education + 
               SelfEmployed + Unemployed + Living + Partisanship + PR_Area, 
             data = UTAS2020)
summary(res2_8)

## Same-sex marriage
res2_9 <- lm(Preference9 ~ Term + Female + Age + Education + 
               SelfEmployed + Unemployed + Living + Partisanship + PR_Area, 
             data = UTAS2020)
summary(res2_9)

## Selective dual-surname system
res2_10 <- lm(Preference10 ~ Term + Female + Age + Education + 
                SelfEmployed + Unemployed + Living + Partisanship + PR_Area, 
              data = UTAS2020)
summary(res2_10)

## plot
res2 <- data.frame(
  Question = rep(1:10, each = 3), 
  Issue = rep(c("Increasing defensive power", "Pre-emptive defense", 
                "Pressue on North Korea", "Small government", 
                "Public works", "Public spending for economic measures", 
                "Consumption tax over 10%", "Security before privacy", 
                "Same-sex marriage", "Selective dual-surname system"), each = 3), 
  Term = rep(c("Term 2 (March 14 - 22)", "Term 3 (March 23 - 26)", 
               "Term 4 (March 27 -)"), times = 10), 
  Effect = c(coef(res2_1)[2:4], coef(res2_2)[2:4], coef(res2_3)[2:4], 
             coef(res2_4)[2:4], coef(res2_5)[2:4], coef(res2_6)[2:4], 
             coef(res2_7)[2:4], coef(res2_8)[2:4], coef(res2_9)[2:4], 
             coef(res2_10)[2:4]), 
  Lower = c(confint(res2_1)[2:4,1], confint(res2_2)[2:4,1], confint(res2_3)[2:4,1], 
            confint(res2_4)[2:4,1], confint(res2_5)[2:4,1], confint(res2_6)[2:4,1], 
            confint(res2_7)[2:4,1], confint(res2_8)[2:4,1], confint(res2_9)[2:4,1], 
            confint(res2_10)[2:4,1]), 
  Upper = c(confint(res2_1)[2:4,2], confint(res2_2)[2:4,2], confint(res2_3)[2:4,2], 
            confint(res2_4)[2:4,2], confint(res2_5)[2:4,2], confint(res2_6)[2:4,2], 
            confint(res2_7)[2:4,2], confint(res2_8)[2:4,2], confint(res2_9)[2:4,2], 
            confint(res2_10)[2:4,2])
)
res2$Color <- "white"
res2$Color[res2$Question == 6] <- "black"
res2_plot <- ggplot(res2, aes(x = reorder(Issue, -Question), y = Effect, 
                              ymax = Upper, ymin = Lower)) + 
  geom_hline(yintercept = 0, linetype = "dashed",colour = "gray") + 
  geom_pointrange(aes(fill = Color), shape = 21) + 
  scale_fill_manual(values = c("black", "white"), guide = FALSE) + 
  facet_wrap(~Term, ncol = 3) + coord_flip() + 
  labs(x = "", y = expression(Support %<-% "Effect" %->% Oppose)) + 
  theme(strip.text.x = element_text(size = 9), 
        axis.title.x = element_text(size = 10), 
        axis.title.y = element_blank(), 
        axis.text.x = element_text(size = 10), 
        axis.text.y = element_text(size = 10))
plot(res2_plot)
